package com.wuliu.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.BasOrderRuleMapper;
import com.wuliu.system.domain.BasOrderRule;
import com.wuliu.system.service.IBasOrderRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 单号规则Service业务层处理
 *
 * @author realue
 * @date 2023-10-24
 */
@Service
public class BasOrderRuleServiceImpl extends ServiceImpl<BasOrderRuleMapper, BasOrderRule> implements IBasOrderRuleService {
    @Resource
    private BasOrderRuleMapper basOrderRuleMapper;

    /**
     * 查询单号规则
     *
     * @param id 单号规则主键
     * @return 单号规则
     */
    @Override
    public BasOrderRule selectBasOrderRuleById(String id) {
        return basOrderRuleMapper.selectBasOrderRuleById(id);
    }

    /**
     * 查询单号规则列表
     *
     * @param basOrderRule 单号规则
     * @return 单号规则
     */
    @Override
    public List<BasOrderRule> selectBasOrderRuleList(BasOrderRule basOrderRule) {
        return basOrderRuleMapper.selectBasOrderRuleList(basOrderRule);
    }

    /**
     * 新增单号规则
     *
     * @param basOrderRule 单号规则
     * @return 结果
     */
    @Override
    public int insertBasOrderRule(BasOrderRule basOrderRule) {
        long count = basOrderRuleMapper.selectCount(new LambdaQueryWrapper<BasOrderRule>()
                .eq(BasOrderRule::getSysAccount, basOrderRule.getSysAccount())
                .eq(BasOrderRule::getPrefix, basOrderRule.getPrefix()));
        if (count > 0) {
            throw new ServiceException("记录重复");
        }
        basOrderRule.setCreateTime(DateUtils.getNowDate());
        return basOrderRuleMapper.insertBasOrderRule(basOrderRule);
    }

    /**
     * 修改单号规则
     *
     * @param basOrderRule 单号规则
     * @return 结果
     */
    @Override
    public int updateBasOrderRule(BasOrderRule basOrderRule) {
        basOrderRule.setUpdateTime(DateUtils.getNowDate());
        return basOrderRuleMapper.updateBasOrderRule(basOrderRule);
    }

    /**
     * 批量删除单号规则
     *
     * @param ids 需要删除的单号规则主键
     * @return 结果
     */
    @Override
    public int deleteBasOrderRuleByIds(String[] ids) {
        return basOrderRuleMapper.deleteBasOrderRuleByIds(ids);
    }

    /**
     * 删除单号规则信息
     *
     * @param id 单号规则主键
     * @return 结果
     */
    @Override
    public int deleteBasOrderRuleById(String id) {
        return basOrderRuleMapper.deleteBasOrderRuleById(id);
    }
}
